package com.example.orderminiappapi.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.example.orderminiappapi.bean.AddOrUpdate;
import com.example.orderminiappapi.bean.Response;
import com.example.orderminiappapi.service.UserAddressService;
import com.example.orderminiappapi.service.UserRemarksService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class AddressOrRemarksController {
    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserRemarksService userRemarksService;

    /**
     * 用户增加其地址
     */
    @ResponseBody
    @RequestMapping(value = "/doAddAddress", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doAddAddress(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userAddressService.addAddress(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "插入失败", "插入失败");
        }

        return response;
    };

    /**
     * 用户修改其地址
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdateAddress", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doUpdateAddress(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userAddressService.updateAddress(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "修改失败", "修改失败");
        }

        return response;
    };

    /**
     * 用户删除其地址
     */
    @ResponseBody
    @RequestMapping(value = "/doDeleteAddress", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doDeleteAddress(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userAddressService.deleteAddress(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "删除失败", "删除失败");
        }

        return response;
    };

    /**
     * 用户改变其常用地址
     */
    @ResponseBody
    @RequestMapping(value = "/changeCommonAddress", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response changeCommonAddress(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag1 = userAddressService.changeCommonAddress1(addOrUpdate);
        int flag2 = userAddressService.changeCommonAddress2(addOrUpdate);

        Response response;

        if (flag2 != 0) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "修改失败", "修改失败");
        }

        return response;
    };

    /**
     * 用户增加其备注
     */
    @ResponseBody
    @RequestMapping(value = "/doAddRemarks", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doAddRemarks(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userRemarksService.addRemarks(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "插入失败", "插入失败");
        }

        return response;
    };

    /**
     * 用户修改其备注
     */
    @ResponseBody
    @RequestMapping(value = "/doUpdateRemarks", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doUpdateRemarks(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userRemarksService.updateRemarks(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "修改失败", "修改失败");
        }

        return response;
    };

    /**
     * 用户删除其备注
     */
    @ResponseBody
    @RequestMapping(value = "/doDeleteRemarks", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response doDeleteRemarks(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag = userRemarksService.deleteRemarks(addOrUpdate);

        Response response;

        if (flag == 1) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "删除失败", "删除失败");
        }

        return response;
    };

    /**
     * 用户改变其常用备注
     */
    @ResponseBody
    @RequestMapping(value = "/changeCommonRemarks", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    private Response changeCommonRemarks(HttpServletRequest request, @RequestBody AddOrUpdate addOrUpdate) throws JsonProcessingException {

        String token = request.getHeader("tokenMiniAPP");

        JWTVerifier build = JWT.require(Algorithm.HMAC256("userInfo")).build();
        DecodedJWT verify = build.verify(token);

        String userId = verify.getClaim("openId").asString();

        addOrUpdate.setId(userId);

        int flag1 = userRemarksService.changeCommonRemarks1(addOrUpdate);
        int flag2 = userRemarksService.changeCommonRemarks2(addOrUpdate);

        Response response;

        if (flag2 != 0) {
            response = new Response("10000", "成功", "成功");
        } else {
            response = new Response("10010", "修改失败", "修改失败");
        }

        return response;
    };
}
